package com.hetao.xx.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.hetao.xx.Global;
import com.hetao.xx.R;
import com.hetao.xx.adapter.MessageAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MessageFragment extends Fragment {

    private View root;
    private Unbinder unbinder;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.message_list)
    ListView listView;

    private MessageAdapter messageAdapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (root == null) {
            root = inflater.inflate(R.layout.fragment_main_message, container, false);
            unbinder = ButterKnife.bind(this, root);
        }

        return root;
    }

    public void init() {
        ArrayList<String> msg = Global.getInstant().msgManage.getMsg();
        messageAdapter = new MessageAdapter(getContext(), R.layout.widget_msg_list_item, msg);
        listView.setAdapter(messageAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //进行判空，避免空指针
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
